package org.jeecg.modules.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.CwJbszqkzb;
import org.jeecg.modules.cw.entity.CwJbszqkzbZhu;
import org.jeecg.modules.cw.entity.CwTj;
import org.jeecg.modules.cw.entity.CwHealthForm;
import org.jeecg.modules.cw.mapper.CwHealthFormMapper;
import org.jeecg.modules.cw.mapper.CwTjMapper;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwJbszqkzbZhuService;
import org.jeecg.modules.cw.service.ICwTjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 提交
 * @Author: jeecg-boot
 * @Date:   2025-09-02
 * @Version: V1.0
 */
@Service
public class CwTjServiceImpl extends ServiceImpl<CwTjMapper, CwTj> implements ICwTjService {
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;
	@Autowired
	private CwTjMapper cwTjMapper;
	@Autowired
	private CwHealthFormMapper cwHealthFormMapper;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ICwJbszqkzbZhuService cwJbszqkzbZhuService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwTj cwTj, List<CwHealthForm> cwHealthFormList) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		cwTj.setUnit(departName);
		cwTjMapper.insert(cwTj);
		if(cwHealthFormList!=null && cwHealthFormList.size()>0) {
			for(CwHealthForm entity:cwHealthFormList) {
				entity.setUnit(cwTj.getUnit());
				entity.setYear(cwTj.getYear());
				entity.setUserId(user.getId());
				//外键设置
				entity.setMainId(cwTj.getId());
				cwHealthFormMapper.insert(entity);
			}


		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwTj cwTj,List<CwHealthForm> cwHealthFormList) {
		cwTjMapper.updateById(cwTj);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//1.先删除子表数据
		cwHealthFormMapper.deleteByMainId(cwTj.getId());
		Double acz=0.0;
		Double afcz=0.0;
		//2.子表数据重新插入
		if(cwHealthFormList!=null && cwHealthFormList.size()>0) {
			for(CwHealthForm entity:cwHealthFormList) {
				//外键设置
				entity.setMainId(cwTj.getId());
				if ("财政".equals(entity.getCzFcz())){
					Double current=entity.getMoney();
					acz = new BigDecimal(acz).add(new BigDecimal(current))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
				}else {
					Double current=entity.getMoney();
					afcz = new BigDecimal(afcz).add(new BigDecimal(current))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
				}
				cwHealthFormMapper.insert(entity);
			}
		}

//		LambdaQueryWrapper<CwJbszqkzb> wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(CwJbszqkzb::getYear,cwTj.getYear())
//				.eq(CwJbszqkzb::getUnit,cwTj.getUnit())
//				.in(CwJbszqkzb::getProject,"医疗费");
//		long count = cwJbszqkzbService.count(wrapper);
//		if (count == 1) {
//			CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper);
//			one1.setUnit(cwTj.getUnit());
//			one1.setYear(cwTj.getYear());
//			one1.setFinancial(acz);
//			one1.setNonFinancials(afcz);
//			if (one1.getYsshj()==null){
//				one1.setYsshj(0.0);
//			}
//			one1.setYsshj(acz+afcz);
//			one1.setCwPx(22);
//			cwJbszqkzbService.updateById(one1);
//		}


	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwHealthFormMapper.deleteByMainId(id);
		cwTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwHealthFormMapper.deleteByMainId(id.toString());
			cwTjMapper.deleteById(id);
		}
	}
	
}
